/*
 * File      : usart.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2009, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2009-01-05     Bernard      the first version
 * 2010-03-29     Bernard      remove interrupt Tx and DMA Rx mode
 * 2012-02-08     aozima       update for F4.
 * 2012-07-28     aozima       update for ART board.
 * 2016-05-28     armink       add DMA Rx mode
 */
#if 0
#include "stm32f4xx.h"
#include "stm32f407xx.h"
#include "usart.h"
#include "stm32f4xx_hal.h"
#include "stm32f4xx_hal_uart.h"
#include <rtdevice.h>

#if defined(RT_USING_UART1)
struct rt_serial_device serial1;
#endif /* RT_USING_UART1 */

/* STM32 uart driver */
extern UART_HandleTypeDef huart1;
static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{

    UART_HandleTypeDef *huart;
    RT_ASSERT(serial != RT_NULL);
    RT_ASSERT(cfg != RT_NULL);

    huart = (UART_HandleTypeDef *)serial->parent.user_data;

    return RT_EOK;
}

static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *arg)
{
#if 0
    struct stm32_uart* uart;
    rt_uint32_t ctrl_arg = (rt_uint32_t)(arg);

    RT_ASSERT(serial != RT_NULL);
    uart = (struct stm32_uart *)serial->parent.user_data;

    switch (cmd)
    {
    case RT_DEVICE_CTRL_CLR_INT:
        /* disable rx irq */
        //UART_DISABLE_IRQ(uart->irq);
        /* disable interrupt */
        //USART_ITConfig(uart->uart_device, USART_IT_RXNE, DISABLE);
        break;
    case RT_DEVICE_CTRL_SET_INT:
        /* enable rx irq */
        //UART_ENABLE_IRQ(uart->irq);
        /* enable interrupt */
        //USART_ITConfig(uart->uart_device, USART_IT_RXNE, ENABLE);
        break;
        /* USART config */
    case RT_DEVICE_CTRL_CONFIG :
        if (ctrl_arg == RT_DEVICE_FLAG_DMA_RX) {
            //DMA_Configuration(serial);
        }
    }
#endif
    return RT_EOK;
}

static int stm32_putc(struct rt_serial_device *serial, char c)
{
    UART_HandleTypeDef *uart;

    RT_ASSERT(serial != RT_NULL);
    uart = (UART_HandleTypeDef *)serial->parent.user_data;

    while (!(uart->Instance->SR & USART_SR_TXE));
    uart->Instance->DR = c;

    return 1;
}
static rt_uint8_t rxBf[16];
static int stm32_getc(struct rt_serial_device *serial)
{
    //int ch;
    UART_HandleTypeDef *uart;

    RT_ASSERT(serial != RT_NULL);
    /*uart = (UART_HandleTypeDef *)serial->parent.user_data;

    ch = -1;

    if (uart->Instance->SR& USART_SR_RXNE)
    {
        ch = uart->Instance->DR & 0xff;
    }
*/
    return rxBf[0];
}

/**
 * Uart common interrupt process. This need add to uart ISR.
 *
 * @param serial serial device
 */
void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
{

    rt_hw_serial_isr(serial1, RT_SERIAL_EVENT_RX_IND);
}


static const struct rt_uart_ops stm32_uart_ops =
{
    stm32_configure,
    stm32_control,
    stm32_putc,
    stm32_getc,
};





int stm32_hw_usart_init(void)
{
    struct stm32_uart *uart;
    struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;


#ifdef RT_USING_UART1
    uart = &uart1;

    serial1.ops    = &stm32_uart_ops;
    serial1.config = config;
    /* register UART1 device */
    rt_hw_serial_register(&serial1,
                          "uart1",
                          RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_DMA_RX,
                          uart);
#endif /* RT_USING_UART1 */
}
INIT_BOARD_EXPORT(stm32_hw_usart_init);
#endif
